package com.woniuxy.mesboot.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.mesboot.dto.ProDTO;
import com.woniuxy.mesboot.dto.ProInfoDTO;
import com.woniuxy.mesboot.entity.ProInf;
import com.woniuxy.mesboot.service.ProInfoService;
import com.woniuxy.mesboot.util.Result;
import com.woniuxy.mesboot.vo.ProInfoVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @author : 黑川
 * @version : 1.0
 * @Project : mes-boot
 * @Package : com.woniuxy.mesboot.controller
 * @ClassName : .java
 * @createTime : 2023/5/24 17:39
 * @Description :
 */
@RestController
@RequestMapping("/proinfo")
public class ProInfoController {
    @Resource
    private ProInfoService infoService;
    @PostMapping("queryAll")
    public Result queryAll(@RequestBody ProInfoDTO infoDTO){
        PageHelper.startPage(infoDTO.getCurrentPage(),infoDTO.getPageSize(),true);
        List<ProInfoVO> list = infoService.selectAll(infoDTO);
        PageInfo<ProInfoVO> pageInfo = new PageInfo<>(list);
        return Result.ok().data("list",list).data("total",pageInfo.getTotal());
    }

    @PostMapping
    public Result add(@RequestBody ProDTO proInf){
        int i = infoService.insertSelective(proInf);
        if(i>0){
            return Result.ok("添加成功");
        }else {
            return Result.error("添加失败");
        }
    }
    @GetMapping("{productId}")
    public Result queryById(@PathVariable("productId") Long id){
        ProInf proInf = infoService.selectByPrimaryKey(id);
        return Result.ok().data("proInf",proInf);
    }
    @PutMapping
    public Result update(@RequestBody ProInf proInf){
        int i = infoService.updateByPrimaryKeySelective(proInf);
        if(i>0){
            return Result.ok("修改成功");
        }else {
            return Result.error("修改失败");
        }
    }
    @DeleteMapping("{productId}")
    public Result delete(@PathVariable("productId") Long id){
        int i = infoService.deleteByPrimaryKey(id);
        if(i>0){
            return Result.ok("删除成功");
        }else {
            return Result.error("删除失败");
        }
    }
}
